package org.dandelion.calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class DayDao {
    private DaySQLiteOpenHelper helper;

    public DayDao(Context context) {
        helper = new DaySQLiteOpenHelper(context);
    }

    public long add(String day, String month, String text) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("day", day);
        values.put("month", month);
        values.put("text", text);
        long id = db.insert("data", null, values);
        db.close();
        return id;
    }

    public int update(int month, int day, String text) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        long id = findIdbyMonthDay(month, day);
        values.put("day", day);
        values.put("month", month);
        values.put("text", text);
        int count = db.update("data", values, "id=?", new String[]{String.valueOf(id)});
        db.close();
        return count;
    }

    public int delete(long id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int count = db.delete("data", "id=?", new String[]{String.valueOf(id)});
        db.close();
        return count;
    }

    public String find(long id) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("data", null, "id=?", new String[]{String.valueOf(id)}, null, null, null);
        boolean result = cursor.moveToNext();
        String info = cursor.getString(2);
        cursor.close();
        db.close();
        return info;
    }

    public String[] findbyMonth(int month) {
        SQLiteDatabase db = helper.getReadableDatabase();
        String day[] = new String[35];
        Cursor cursor = db.query("data", null, "month=?", new String[]{"" + month}, null, null, null);
        String str = "";
        int i = 0;
        while (cursor.moveToNext()) {
            day[i] = cursor.getString(2);
            Log.d("abba", i + "," + month + "," + day[i]);
            i++;
        }
        cursor.close();
        return day;
    }

    public String findbyMonthDay(int month, int day) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Log.d("aaaaa", "" + month + "," + day);
        Cursor cursor = db.query("data", null, "month=? and day=?", new String[]{"" + month, "" + day}, null, null, null);
        String str = "";
        cursor.moveToNext();
        str = str + cursor.getString(3);
        cursor.close();
        return str;
    }

    public long findIdbyMonthDay(int month, int day) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Log.d("aaaaa", "" + month + "," + day);
        Cursor cursor = db.query("data", null, "month=? and day=?", new String[]{"" + month, "" + day}, null, null, null);
        cursor.moveToNext();
        long id = cursor.getLong(0);
        Log.d("id是", "" + id);
        cursor.close();
        return id;
    }
}
